package com.be.beadmin.quality.controller;

import com.be.beadmin.quality.dto.input.ComplaintFileInfoPage;
import com.be.beadmin.quality.entity.ComplaintFileInfo;
import com.be.beadmin.quality.dto.output.ComplaintFileInfoEx;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Page;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.quality.service.ComplaintFileInfoService;
import com.be.beadmin.quality.dto.input.ComplaintFileInfoQueryCriteria;
import com.be.beadmin.quality.dto.input.ComplaintFileInfoDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import com.alibaba.excel.EasyExcel;
import org.springframework.web.multipart.MultipartFile;

/**
* @author twl
* @date 2023-09-21
**/
@Api(tags = "质量管理-客户投诉")
@RestController
public class ComplaintFileInfoController {

    private String bucketName = "complaintflie";
    @Autowired
    private  ComplaintFileInfoService complaintFileInfoService;


    @PostMapping(value = "/quality/getComplaintFileInfoByPage")
    public ResultModel getComplaintFileInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(complaintFileInfoService.getComplaintFileInfoByPage(pageEntity)) ;
    }

    @ApiOperation(value="获取附件分页（By ComplaintId工单号）")
    @PostMapping(value="/quility/getComplaintFileInfoByPageAndCP")
    public ResultModel getComplaintFileInfoByPageAndCP(@RequestBody ComplaintFileInfoPage complaintFileInfoPage){
        return ResultModel.success(complaintFileInfoService.getComplaintFileInfoByPageAndCP(complaintFileInfoPage));
    }

    @PostMapping("/quality/addComplaintFileInfo")
    public ResultModel addComplaintFileInfo(@ApiParam(value = "投诉单ID",required = true) @RequestParam(required = true) String complaintId,
                                            @ApiParam(value = "问题图片上传",required = true) @RequestParam(required = false,value="problemPicFile") MultipartFile[] multipartFiles,
                                            @ApiParam(value = "投诉单ID",required = true) @RequestParam(required = true) Integer state
    ){
        complaintFileInfoService.addComplaintFileInfo(complaintId,multipartFiles,state,bucketName);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/quality/delComplaintFileInfo")
    public ResultModel delComplaintFileInfo(@RequestBody ComplaintFileInfoDto complaintFileInfoDto){
        complaintFileInfoService.delComplaintFileInfo(complaintFileInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/quality/updateComplaintFileInfo")
    public ResultModel updateComplaintFileInfo(@RequestBody ComplaintFileInfoDto complaintFileInfoDto){
        complaintFileInfoService.updateComplaintFileInfo(complaintFileInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/quality/exportComplaintFileInfo")
    public void exportComplaintFileInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<ComplaintFileInfoEx> complaintFileInfoPage = complaintFileInfoService.getComplaintFileInfoByPage(pageEntity);
        List<ComplaintFileInfoEx> complaintFileInfoExes = complaintFileInfoPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("ComplaintFileInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ComplaintFileInfoEx.class).sheet("sheet1").doWrite(complaintFileInfoExes);
    }
}